// @ts-ignore
import * as React from 'react';

// @ts-ignore
import {StyleSheet, View, Text, Button, Alert} from 'react-native';
import {
  checkMicrophone,
  requestMicrophone,
  requestCamera,
  requestPhotoLibrary,
  openLimitedPhotoLibraryPicker,
  openSettings,
  requestMediaLibrary,
  requestCoreLocation,
  requestBluetooth
} from 'react-native-check-permissions';

export default class App extends React.Component<any, any> {
  constructor(r: any) {
    super(r);
    this.state = {
      result: '',
    }
  }

  render() {


    return (
      <View style={styles.container}>
        <Text>Result: {this.state.result}</Text>
        <Button title={'设置'} onPress={() => {
          openSettings().then(res => {
            console.log(res);
            // alert(res);
          })
        }}/>
        <Button title={'麦克风'} onPress={() => {
          requestMicrophone().then(res => {
            this.showResult(res);
          })
        }}/>
        <Button title={'相机'} onPress={() => {
          requestCamera().then(res => {
            this.showResult(res);
          })
        }}/>
        <Button title={'相册'} onPress={() => {
          requestPhotoLibrary().then(res => {
            this.showResult(res);
          })
        }}/>
        <Button title={'打开有限的相册'} onPress={() => {
          openLimitedPhotoLibraryPicker().then(res => {
            this.showResult(res);
          }).catch(e => {
            console.log(e.message);
          })
        }}/>
        <Button title={'多媒体'} onPress={() => {
          requestMediaLibrary().then(res => {
            this.showResult(res);
          }).catch(e => {
            console.log(e.message);
          })
        }}/>
        <Button title={'位置'} onPress={() => {
          requestCoreLocation().then(res => {
            this.showResult(res);
          }).catch(e => {
            console.log(e.message);
          })
        }}/>
        <Button title={'蓝牙'} onPress={() => {
          requestBluetooth().then(res => {
            this.showResult(res);
          }).catch(e => {
            console.log(e.message);
          })
        }}/>

      </View>
    );
  }

  showResult = (res: any) => {
    let {status} = res;
    switch (status) {
      case 0:
        // 不可用
        break;
      case 1:
        // 未定
        break;
      case 2:
        // 受限制的
        break;
      case 3:
        // 拒绝
        Alert.alert('提示', '无权限，请打开设置！', [{text: '取消'}, {
          text: '设置', onPress: () => {
            openSettings().then(res => {
            });
          }
        }])
        break;
      case 4:
        // 已认证
        break;
      case 5:
        // 有限的
        break;
      default:
        break;
    }
    this.setState({
      result: status
    })
  }


  componentDidMount() {
  }
}


const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  box: {
    width: 60,
    height: 60,
    marginVertical: 20,
  },
});
